/**
 * @description: 「日程日历」组件数据生成相关方法
 * @author: Ruan Jiazhen
 * @date: 2024-03-29 13:39:59
 **/

import dayjs from 'dayjs';

export const PANEL_DAYS = 42;

type generateDayInfoParam = {
  year: number;
  month: number;
  day: number;
  isCurrentMonth?: boolean;
  checkIsToday?: boolean;
  markList?: Array<Mark>;
};

/** 生成单个日期数据
 * @param param
 */
export const generateDayInfo = (param: generateDayInfoParam) => {
  const { year, month, day, isCurrentMonth, checkIsToday, markList } = {
    isCurrentMonth: false,
    checkIsToday: false,
    markList: [],
    ...param,
  };

  // const checkIsToday = param?.checkIsToday || false;
  // const markList = param?.markList || [];

  const dateStr = `${year}-${month.toString().padStart(2, '0')}-${day.toString().padStart(2, '0')}`;

  const mark = markList.find((item) => {
    return dayjs(item.date).isSame(dateStr);
  });

  return {
    year,
    month,
    day,
    isCurrentMonth,
    isToday: checkIsToday ? dayjs().isSame(dateStr, 'day') : false,
    mark,
  };
};

// XXX 添加是否从周日开始的配置
/**
 * @description: 生成单个面板的日历数据
 * @param {year: number, month: number} date
 */
export const generatePanelDayList = (date: { year: number; month: number }, markList = [] as Array<Mark>) => {
  const { year, month } = date;
  const daysInMonth = dayjs(`${year}-${month}-01`).daysInMonth();

  const firstDayWeek = dayjs(`${year}-${month}-01`).day();
  const prevMonthDays = firstDayWeek;
  const prevMonth = month === 1 ? 12 : month - 1;
  const prevYear = month === 1 ? year - 1 : year;
  const prevDaysInMonth = dayjs(`${prevYear}-${prevMonth}-01`).daysInMonth();
  const prevMonthDaysData = Array.from({ length: prevMonthDays }, (_, k) => {
    const day = prevDaysInMonth - prevMonthDays + k + 1;
    return generateDayInfo({
      year: prevYear,
      month: prevMonth,
      day,
      markList,
    });
  });

  const nextMonthDays = PANEL_DAYS - prevMonthDays - daysInMonth;
  const nextMonth = month === 12 ? 1 : month + 1;
  const nextYear = month === 12 ? year + 1 : year;
  const nextMonthDaysData = Array.from({ length: nextMonthDays }, (_, k) => {
    const day = k + 1;
    return generateDayInfo({
      year: nextYear,
      month: nextMonth,
      day,
      markList,
    });
  });

  const currentMonthDaysData = Array.from({ length: daysInMonth }, (_, k) => {
    const day = k + 1;

    return generateDayInfo({
      year,
      month,
      day,
      isCurrentMonth: true,
      checkIsToday: true,
      markList,
    });
  });

  return [...prevMonthDaysData, ...currentMonthDaysData, ...nextMonthDaysData];
};

// XXX 添加是否从周日开始的配置
/**
 * @description: 生成swiper的日历数据
 * @param {year: number, month: number} date
 */
export const generateSwiperPanelData = (date: { year: number; month: number }, markList = [] as Array<Mark>) => {
  const { year, month } = date;
  const firstPanelData = generatePanelDayList(date, markList);

  const nextYear = month === 12 ? year + 1 : year;
  const nextMonth = month === 12 ? 1 : month + 1;
  const nextPanelData = generatePanelDayList({ year: nextYear, month: nextMonth }, markList);

  const prevYear = month === 1 ? year - 1 : year;
  const prevMonth = month === 1 ? 12 : month - 1;
  const prevPanelData = generatePanelDayList({ year: prevYear, month: prevMonth }, markList);
  return [
    {
      year,
      month,
      dayList: firstPanelData,
    },
    {
      year: nextYear,
      month: nextMonth,
      dayList: nextPanelData,
    },
    {
      year: prevYear,
      month: prevMonth,
      dayList: prevPanelData,
    },
  ];
};
